package com.core.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.DAOException;
import com.core.dao.DAOFactory;
import com.core.dao.impl.OnlineAccountDAOImpl;
import com.core.entities.OnlineAccount;

public class OnlineAccountSrv {
	
	private Connection conn;
	private OnlineAccountDAOImpl onlineAccountDAOImpl;
	private OnlineAccount account;

	public OnlineAccountSrv(Connection conn) {
		this.conn = conn;
	}
	
	public List<String> getOnlineAccounts(){
		List<String> onlineAccountsNames = new ArrayList<String>();
		try{
			for(OnlineAccount onlineAccount : DAOFactory.getInstance().getOnlineAccountDAO(conn).selectAll())
				
				onlineAccountsNames.add(onlineAccount.getFacebook() + onlineAccount.getGoogle() + onlineAccount.getTwitter());

		}catch(DAOException e){
			e.printStackTrace();
		}
		return onlineAccountsNames;
	}
	
	public List<String> getFacebookOnlineAccount(String facebookName){
		List<String> facebookNames = new ArrayList<String>();
		try{
			for(OnlineAccount onlineAccount : DAOFactory.getInstance().getOnlineAccountDAO(conn).getByFacebook(facebookName))
			facebookNames.add(onlineAccount.getFacebook());

		}catch(DAOException e){
			e.printStackTrace();
		}
		return facebookNames;
	}
	public List<String> getTwitterOnlineAccount(String twitterName){
		List<String> twitterNames = new ArrayList<String>();
		try{
			for(OnlineAccount onlineAccount : DAOFactory.getInstance().getOnlineAccountDAO(conn).
					getByTwitter(twitterName))
			
			twitterNames.add(onlineAccount.getTwitter());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return twitterNames;
	}
	public List<String> getGoogleOnlineAccount(String googleName){
		List<String> googleNames = new ArrayList<String>();
		try{
			for(OnlineAccount onlineAccount : DAOFactory.getInstance().getOnlineAccountDAO(conn).
					getByGoogle(googleName))
			
			googleNames.add(onlineAccount.getGoogle());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return googleNames;
	}
	public int getFacebookOnlineAccountPk(String facebookName){
		try{
			return DAOFactory.getInstance().getOnlineAccountDAO(conn).getByFacebook(facebookName).get(0).getId();
		}catch(DAOException e){
			e.printStackTrace();
		}
		return -1;
	}
	public int getTwitterOnlineAccountPk(String twitterName){
		try{
			return DAOFactory.getInstance().getOnlineAccountDAO(conn).getByTwitter(twitterName).get(0).getId();
		}catch(DAOException e){
			e.printStackTrace();
		}
		return account.getId();
	}
	public int getGoogleOnlineAccountPk(String googleName){
		try{
			return DAOFactory.getInstance().getOnlineAccountDAO(conn).getByGoogle(googleName).get(0).getId();
		}catch(DAOException e){
			e.printStackTrace();
		}
		return account.getId();
	}
	public OnlineAccountDAOImpl getOnlineAccountDAO() throws DAOException{
		if(onlineAccountDAOImpl == null)
			onlineAccountDAOImpl = (OnlineAccountDAOImpl) DAOFactory.getInstance().getOnlineAccountDAO(conn);
		return onlineAccountDAOImpl;
	}
	
}
